package com.gitee.magic.framework.head.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import com.gitee.magic.core.exception.ApplicationException;
import com.gitee.magic.core.utils.ClassLoaderUtils;
import com.gitee.magic.core.utils.codec.Base64;
import com.gitee.magic.core.utils.codec.Des;
import com.gitee.magic.core.utils.codec.HmacSha1;
import com.gitee.magic.core.utils.codec.Rc4;

/**
 * 编码处理类
 * 
 * @author Start
 */
public class CodecUtils {

	/**
	 * HmacSHA1签名
	 */
	public static String signatureHmacSha1(String key, String data) {
		try {
			return Base64.encode(HmacSha1.signature(data, key));
		} catch (InvalidKeyException | NoSuchAlgorithmException e) {
			throw new ApplicationException(e);
		}
	}

	/**
	 * DES加密
	 */
	public static String doKeyEncrypt(String str,String classpath) {
		try {
			return Base64.encode(Des.encrypt(str, ClassLoaderUtils.getResourcePath(classpath)));
		} catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | ClassNotFoundException
				| NoSuchAlgorithmException | NoSuchPaddingException | IOException e) {
			throw new ApplicationException(e);
		}
	}

	/**
	 * DES解密
	 */
	public static String doKeyDecrypt(String str,String classpath) {
		try {
			return Des.decrypt(Base64.decode(str), ClassLoaderUtils.getResourcePath(classpath));
		} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | ClassNotFoundException
				| IllegalBlockSizeException | BadPaddingException | IOException e) {
			throw new ApplicationException(e);
		}
	}

	/**
	 * URL编码
	 */
	public static String encode(String str,String encoding) {
		try {
			return URLEncoder.encode(str, encoding);
		} catch (UnsupportedEncodingException e) {
			throw new ApplicationException(e);
		}
	}

	/**
	 * URL解码
	 */
	public static String decode(String str,String encoding) {
		try {
			return URLDecoder.decode(str, encoding);
		} catch (UnsupportedEncodingException e) {
			throw new ApplicationException(e);
		}
	}

	/**
	 * RC4加密
	 * @param content 原始内容，需要进行加密的数据
	 * @param key
	 * @return
	 */
	public static String rc4Encrypt(String content, String key,String encoding){
		try {
			return Rc4.encode(content.getBytes(encoding), key);
		} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException
				| BadPaddingException | UnsupportedEncodingException e) {
			throw new ApplicationException(e);
		}
	}
	
	/**
	 * RC4解密
	 * @param content 加密后的内容
	 * @param key
	 * @return
	 */
	public static String rc4Decrypt(String content, String key) {
		try {
			return new String(Rc4.decode(content, key));
		} catch (InvalidKeyException | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException
				| NoSuchAlgorithmException | NoSuchPaddingException e) {
			throw new ApplicationException(e);
		}
	}

}